package com.decades.application.main;

import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.aphidmobile.flip.FlipViewController;
import com.decades.application.adapter.FlipViewAdapter;
import com.decades.application.dto.BlogDto;
import com.decades.application.dto.SlideDto;
import com.decades.application.json.JSONParserForGetList;
import com.decades.application.model.Menu;
import com.decades.application.model.User;
import com.jakewharton.salvage.ProgressHUD;
import com.roscopeco.ormdroid.ORMDroidApplication;

@SuppressLint("NewApi")
public class NrctMainActivity extends Activity {
	private final Handler handler = new Handler();
	private FlipViewController flipView;
	private Drawable oldBackground = null;
	private int currentColor = 0xFF666666;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
	    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		ORMDroidApplication.initialize(NrctMainActivity.this);
		System.gc();
		flipView = new FlipViewController(this);
		flipView.setAnimationBitmapFormat(Bitmap.Config.RGB_565);
		new InitFlipView().execute();		
		setContentView(flipView);
	}
	private Drawable.Callback drawableCallback = new Drawable.Callback() {
		@Override
		public void invalidateDrawable(Drawable who) {
			getActionBar().setBackgroundDrawable(who);
		}

		@Override
		public void scheduleDrawable(Drawable who, Runnable what, long when) {
			handler.postAtTime(what, when);
		}

		@Override
		public void unscheduleDrawable(Drawable who, Runnable what) {
			handler.removeCallbacks(what);
		}
	};

	@Override
	protected void onResume() {
		super.onResume();
		flipView.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
		flipView.onPause();
	}
	public class InitFlipView extends AsyncTask<String, Void, List<Menu>> implements OnCancelListener{
		ProgressHUD mProgressHUD;
		List<SlideDto> list;
		List<BlogDto> imgList;
		
    	@Override
    	protected void onPreExecute() {
        	mProgressHUD = ProgressHUD.show(NrctMainActivity.this,"Loading Content...", true,true,this);
    		super.onPreExecute();
    	}
		@Override
		protected List<Menu> doInBackground(String... params) {
			// TODO Auto-generated method stub
			//JSONParserForGetList json =  JSONParserForGetList.getInstance();
			Menu menu = new Menu();
			
			List<Menu> menuList = menu.getMenuSelected();
			if(menuList==null || menuList.isEmpty()){
				User user = new User();
				String token = "";
				if(user.getUserLogin()!=null){
					
					token = user.getUserLogin().token;
				}
				JSONParserForGetList.getInstance().getMenus(token);
				menuList = menu.getMenuSelected();
			}
			list = JSONParserForGetList.getInstance().getSlides();
			imgList = JSONParserForGetList.getInstance().getImageFiftySlide();
			return menuList;
		}
		
		@Override
		protected void onPostExecute(List<Menu> result) {
			super.onPostExecute(result);
			if (result != null) {
				flipView.setAdapter(new FlipViewAdapter(NrctMainActivity.this,result,list,imgList));
				mProgressHUD.dismiss();
			} else {
				mProgressHUD.dismiss();
				(Toast.makeText(getApplicationContext(), "An error occured.", Toast.LENGTH_LONG)).show();
			}
			
		}
		@Override
		public void onCancel(DialogInterface dialog) {
			// TODO Auto-generated method stub
			mProgressHUD.dismiss();
		}

	}
}
